﻿using AppConfig;
using IdentityServer4.Models;
using IdentityServer4.Test;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static IdentityModel.OidcConstants;
using static System.Formats.Asn1.AsnWriter;

namespace Common.Identity
{
    public class IdentityConfig
    {
        /// <summary>
        /// Api资源 静态方式定义
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource(OAuthClientConfig.ApiName,OAuthClientConfig.ApiName),
            };
        }
        public static IEnumerable<ApiScope> GetApiScopes()
        {
            
            var scopes = new List<ApiScope>
            {
                //new ApiScope("user_api","user_api"),
                //new ApiScope("token","token"),
                new ApiScope(ApplicationConfig.Gateway.ServiceName)
            };
            foreach (var item in ApplicationConfig.ApplicationEntitys)
            {
                scopes.Add(new ApiScope(item.ServiceName));
            }
            return scopes;
        }
        //public static IEnumerable<IdentityResource> GetIdentityResources()
        //{
        //    return new IdentityResource[]
        //    {
        //        new IdentityResources.OpenId()
        //    };
        //}

        public static IEnumerable<Client> GetClients()
        {
            List<string> Scopes = GetApiScopes().Select(t => t.Name).ToList();
            Scopes.Add(StandardScopes.OfflineAccess);
            return new List<Client>
            {
                new Client()
                {
                    ClientId =OAuthClientConfig.ClientId,
                    AllowedGrantTypes = IdentityServer4.Models.GrantTypes.ResourceOwnerPassword,
                    ClientSecrets = {new Secret(OAuthClientConfig.Secret.Sha256()) },
                    AllowedScopes= Scopes,
                    AccessTokenLifetime = OAuthServerConfig.ExpireIn,
                    AllowOfflineAccess = true,//如果要获取refresh_tokens ,必须把AllowOfflineAccess设置为true
                },
            };
        }
        ///// <summary>
        ///// 测试的账号和密码
        ///// </summary>
        ///// <returns></returns>
        //public static List<TestUser> GetTestUsers()
        //{
        //    return new List<TestUser>
        //    {
        //        new TestUser()
        //        {
        //             SubjectId = "1",
        //             Username = "test",
        //             Password = "123456"
        //        }
        //    };
        //}
    }
}
